/**
 * @file LocalStatic.h
 * @author haiming
 * @brief 饿汉模式实现单例模式
 * 特点：线程安全；多线程下也不存在内存泄漏；不管有没有调用GetInstance函数，只要加载该模块，就会创建一个实例
 * 缺陷：创建的实例有可能为空(vs2010上32位编译平台必现，Linux下都没有出现)，
 *       且网上说是C++11 才实现的线程安全，笔者使用-std=c++98测试发现也是满足线程安全的
 * @version 0.1
 * @date 2022-08-05
 * 
 * @copyright Copyright (c) 2022
 * 
 */

#pragma once
#include <stdio.h>
class LocalStatic
{
public:
    static LocalStatic* GetInstance();


private:
    LocalStatic(void);
    LocalStatic(const LocalStatic&){}
    LocalStatic operator= (const LocalStatic&){}
    ~LocalStatic(void);
    
    class _Inner{
    public:
        _Inner(){printf("create local static inner.\n");};
        ~_Inner(){
            printf("destroy inner.\n");
            LocalStatic* instance = GetInstance();
            if(NULL != instance){
                delete instance;
                instance = NULL;
            }
        }
    };

    static _Inner m_inner;
};

